package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.util.ConfigUtil;

import com.util.BODY;
import com.util.HttpRequest;
import com.util.JSONUtil;
import com.util.OpenKey;
import com.util.ReadConfig;
import com.util.SessionInfo;

/**
 * 缴存历史记录
 * @author 
 */
@Controller
@RequestMapping("/dwrybjxxcontroller")
public class dwrybjxxcontroller {

	private static final Logger logger = Logger.getLogger(dwrybjxxcontroller.class);
	Map map  = new HashMap();
	List list = new ArrayList();
	  
	@RequestMapping("/test")
	@ResponseBody
	
	public String entryinterface(HttpServletRequest request,HttpSession session, HttpServletResponse response ,String limit ,String id,String start,String end,String RQ,String SJ) throws IOException{
		//读取配置文件路劲
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		
		String interfaceUrl = ReadConfig.readValue();
		int a=0;
		int b=0;
		if(RQ!="" && RQ!=null){
			 a= Integer.parseInt(RQ.replaceAll("-", ""));//时间的转换
		}
		if(SJ!="" && SJ!=null){
			b= Integer.parseInt(SJ.replaceAll("-", ""));
		}
		System.out.println(a);
		System.out.println(b);
		Map reqMap = new HashMap();
		BODY by=new BODY();
		OpenKey opk=new OpenKey();
		//接口编码
		reqMap.put("AUTHORITY", "YBGJJ171");
		//接口参数
		reqMap.put("LOGUserName","admin");
		reqMap.put("OpenKey",sessionIn.getKEY());
		by.setDWZH(sessionIn.getDWZH());
		reqMap.put("BODY",by);
		String  result = opk.getOpenKey(reqMap, session);
		Map retMap = JSONUtil.toMap(result);
		logger.info("接口返回内容:"+retMap);
		if(retMap.get("RETURNCODE").equals("100")){
		retMap = (Map) retMap.get("body");
    	List retlist = (List)retMap.get("RETDATA");
		logger.info("接口返回内容:"+result);
			Map resMap = JSONUtil.toMap(result);
					List listttmp = new ArrayList();
					for(int i=0;i<=retlist.size()-1;i++){
					  Map map=(Map)retlist.get(i);
						if(a!=0 && b!=0){
							int num= Integer.parseInt(map.get("BJRQ").toString().replaceAll("-", ""));
							System.out.println(num);
							if(num>b && num<a){	
								String g="-";
								String sum=String.valueOf(num);
								sum=sum.substring(0,4)+g+sum.substring(4,6)+g+sum.substring(6,8);
								if(sum.equals(map.get("BJRQ").toString())){
									listttmp.add(map);
									continue;
								}
								
							}
						 }else{
							 listttmp.add(map);
						 }
						
					}
					if(listttmp.size()>0){
						map.put("code", 0); 
						map.put("msg", null);
						map.put("data",listttmp);
						list.add(resMap);
						response.setHeader("Content-type", "text/html;charset=UTF-8");
						response.setCharacterEncoding("UTF-8");
						String jsonString = JSONUtil.ToJsongString2(map);
						PrintWriter pw = response.getWriter();
						pw.write(jsonString);
					}else{
						map.put("code", 1);
						map.put("CW", "当前条件无信息");
						response.setHeader("Content-type", "text/html;charset=UTF-8");
						response.setCharacterEncoding("UTF-8");
						String jsonString = JSONUtil.ToJsongString2(map);
						PrintWriter pw = response.getWriter();
						pw.write(jsonString);
						return null;
					}
					
		}else{
			map.put("code", 1);
			map.put("CW", retMap.get("RETURNDESC").toString());
			response.setHeader("Content-type", "text/html;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			String jsonString = JSONUtil.ToJsongString2(map);
			PrintWriter pw = response.getWriter();
			pw.write(jsonString);
			return null;
		}
		return null;
	}
}
